package com.virtualtrainer_01.view;

import java.util.ArrayList;
import java.util.List;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

import com.virtualtrainer_01.R;
import com.virtualtrainer_01.dao.ClientDB;
import com.virtualtrainer_01.dao.GroupDB;
import com.virtualtrainer_01.model.Client;
import com.virtualtrainer_01.model.Group;
import com.virtualtrainer_01.util.NameList;
import com.virtualtrainer_01.viewListeners.CheckableLinearLayout;
import com.virtualtrainer_01.viewListeners.SetupListAdapter;

public class SetupTraining extends ExpandableListActivity implements
		OnClickListener {

	private ExpandableListView exp;
	private Spinner spinner;
	private String[] days;
	private ClientDB clientDB;
	private Client client;
	private ImageButton addNewGroup;
	private Button saveTraining;
	private NameList<Group> grupe;
	private List<NameList<Group>> groupsToInsert = new ArrayList<NameList<Group>>();
	private NameList<Group> listg1 = new NameList<Group>();
	private NameList<Group> listg2 = new NameList<Group>();
	private ArrayAdapter<String> adapter;

	public void addListenerOnSpinnerItemSelection() {

		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {

				exp = (ExpandableListView) findViewById(android.R.id.list);
				exp.setAdapter(new SetupListAdapter(SetupTraining.this,
						groupsToInsert, spinner.getSelectedItemPosition()));
				exp.setOnChildClickListener(new OnChildClickListener() {
					View convertView;

					@Override
					public boolean onChildClick(ExpandableListView parent,
							View v, int groupPosition, int childPosition,
							long id) {
						try {
							// String exName = ((SetupListAdapter) exp
							// .getExpandableListAdapter()).getChildList()[groupPosition][childPosition];
							// Intent intent = new Intent(SetupTraining.this,
							// Description.class);
							// intent.putExtra("cheia", exName);
							// startActivityForResult(intent, 1);
							if (convertView == null)
								convertView = getLayoutInflater().inflate(
										R.layout.list_item, parent, false);
							//((CheckableLinearLayout)convertView).setChecked(true);
							//((ImageView) convertView.findViewById(R.id.ImageId))
								//	.setImageResource(R.drawable.checkbox_checked_hi);
						} catch (Exception ex) {
							ex.printStackTrace();
							Toast.makeText(SetupTraining.this, "Error",
									Toast.LENGTH_LONG).show();
						}
						return false;
					}
				});
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setuptraining);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		addNewGroup = (ImageButton) findViewById(R.id.b_add_group);
		saveTraining = (Button) findViewById(R.id.b_save_training);
		registerForContextMenu(addNewGroup);

		spinner = (Spinner) findViewById(R.id.spinner);

		clientDB = new ClientDB(this);
		clientDB.open();
		client = clientDB.getClient();
		clientDB.close();

		GroupDB gdb = new GroupDB(this);
		gdb.open();
		grupe = gdb.getGroupList();
		gdb.close();

		days = new String[client.getDisponibility()];
		for (int i = 0; i < client.getDisponibility(); i++) {
			days[i] = "Day " + (i + 1);
		}

		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, days);
		spinner.setAdapter(adapter);
		groupsToInsert.add(listg1);
		groupsToInsert.add(listg2);
		addListenerOnSpinnerItemSelection();

	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Select Group");

		for (int i = 0; i < grupe.size(); i++) {
			menu.add(i, v.getId(), 0, grupe.get(i).getName());
		}
	}

	public boolean onContextItemSelected(MenuItem item) {
		contextFunction(item.getGroupId());
		int day = spinner.getSelectedItemPosition();
		spinner.setAdapter(adapter); // necesar pentru a reseta adaptarul
		// ca sa adauge grupele cand trebuie, sa nu selectezi ziua pt o popula
		// lista de grupe
		spinner.setSelection(day);
		return true;
	}

	private void contextFunction(int itemId) {
		for (int i = 0; i < grupe.size(); i++) {
			if (itemId == i)
				groupsToInsert.get(spinner.getSelectedItemPosition()).add(
						grupe.get(i));
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.b_save_training:
			Toast.makeText(this, "save", Toast.LENGTH_SHORT).show();
			break;
		case R.id.b_add_group:
			Toast.makeText(this, "click", Toast.LENGTH_SHORT).show();
			break;
		}
	}
}
